package com.service;

import static com.def.GenericDef.BANK_IN;
import static com.def.GenericDef.BOX_INVALIED;
import static com.def.GenericDef.EVT_CHNAGE_LOT;
import static com.def.GenericDef.EVT_LGON;
import static com.def.GenericDef.E_BIS_DATA;
import static com.def.GenericDef.E_BIS_PATHI;
import static com.def.GenericDef.E_BIS_TOOL_GR;
import static com.def.GenericDef.E_READ_NOT_FOUND;
import static com.def.GenericDef.E_RET_BOX;
import static com.def.GenericDef.E_RET_PRD_INFO;
import static com.def.GenericDef.E_XPMOVEIN_BOX_AND_PRD_COUNT_NOT_MATCH;
import static com.def.GenericDef.E_XPMOVEIN_BOX_ID_EMPTY;
import static com.def.GenericDef.E_XPMOVEIN_BOX_OPE_NOT_MATCH;
import static com.def.GenericDef.E_XPMOVEIN_BOX_STATUS_INVALIED;
import static com.def.GenericDef.E_XPMOVEIN_BOX_TOOLG_NOT_MATCH;
import static com.def.GenericDef.E_XPMOVEIN_INVALIED_DATA_SET_PRST;
import static com.def.GenericDef.E_XPMOVEIN_PRD_IN_WIP_BNK;
import static com.def.GenericDef.E_XPMOVEIN_PRD_OPE_NOT_MATCH;
import static com.def.GenericDef.E_XPMOVEIN_PROC_CHANGE_NOT_WIP_IN_OUT;
import static com.def.GenericDef.E_XPMOVEIN_SHT_MIX_NX_OPE_ID;
import static com.def.GenericDef.E_XPMOVEIN_SHT_MIX_NX_OPE_NO;
import static com.def.GenericDef.E_XPMOVEIN_SHT_MIX_NX_OPE_VER;
import static com.def.GenericDef.E_XPMOVEIN_SHT_MIX_NX_PATH_ID;
import static com.def.GenericDef.E_XPMOVEIN_SHT_MIX_NX_PATH_VER;
import static com.def.GenericDef.E_XPMOVEIN_SHT_MIX_NX_PROC_ID;
import static com.def.GenericDef.E_XPMOVEIN_SHT_MIX_NX_TOOLG_ID;
import static com.def.GenericDef.E_XPMOVEIN_SHT_STATUS_INVALIED;
import static com.def.GenericDef.PATH_IS_FINISH;
import static com.def.GenericDef.PATH_TYPE_MAIN;
import static com.def.GenericDef.PROC_IN_PROCESS;
import static com.def.GenericDef.PROC_WAIT;
import static com.def.GenericDef.RETURN_CODE_OK;
import static com.def.GenericDef.RETURN_CODE_SET_ERROR;
import static com.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.def.GenericDef.RETURN_MESG_OK;
import static com.def.GenericDef.TOOL_TYPE_COMMONE;
import static com.def.GenericDef.TOOL_TYPE_IDENPENDENT;
import static com.def.GenericDef.TRX_IN;
import static com.def.GenericDef.TRX_OUT;
import static com.def.GenericDef.T_XPINQCOD;
import static com.def.GenericDef.T_XPMOVEIN;
import static com.def.GenericDef._NORMAL;
import static com.def.GenericDef._SPACE;
import static com.def.GenericDef._YES;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.dao.*;
import com.model.*;
import org.apache.log4j.Logger;
import org.aspectj.apache.bcel.generic.RET;
import org.hibernate.LockMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.icim.service.ICIMBaseService;
import com.rtnBean.NotFoundRtnBeen;
import com.rtnBean.RouteObj;
import com.rtnBean.SimpleRtnBeen;
import com.tx.Xpinqcod.XpinqcodI;
import com.tx.Xpinqcod.XpinqcodO;
import com.tx.Xpmovein.XpmoveinI;
import com.tx.Xpmovein.XpmoveinO;
import com.util.GUIDGenerator;
import com.util.MqOpe;
import com.util.Util;

@Scope("prototype")
@Service("XpmoveinService")
public class XpmoveinService extends ICIMBaseService{
	private static Logger logger = Logger.getLogger(XpmoveinService.class);
	private static String TX_NAME = T_XPMOVEIN;

	private XpmoveinI inTrx;
	private XpmoveinO outTrx;
	private MqOpe mqOpe;

	private String gszMsgBuf = "";
	private Timestamp gEvtTimestamp;
	private Double gEvtTimeD;
	private String gEvtSeqID;

	private String gToolG;
	private Bis_tool gBis_tool;
	private Ret_box gRet_box;
	private List<Ret_prd_info> gRet_prd_infoList;

	private Bis_path gNx_bis_path;
	private Bis_pathi gNx_bis_pathi;
	private Bis_ope gNx_bis_ope;
	private RouteObj gNxRouteObj;

	private String gtoolType = "";

	@Autowired
	private IBisToolDao bisToolDao;
	@Autowired
	private IBisToolGrDao bisToolGrDao;
	@Autowired
	private IRetToolStDao retToolStDao;
	@Autowired
	private IRetBoxDao retBoxDao;
	@Autowired
	private IRetPrdInfoDao retPrdInfoDao;
	@Autowired
	private IRetPrdInDao retPrdInDao;
	@Autowired
	private IRetWODao retWoDao;
	@Autowired
	private IBisMdlDefDao bisMdlDefDao;
	@Autowired
	private IBisOpeDao bisOpeDao;
	@Autowired
	private IBisPathDao bisPathDao;
	@Autowired
	private IBisPathiDao bisPathiDao;

	@Autowired
	private XpinqcodService xpinqcodService;

	public String subMainProc(String strInMsg) {
		long rtnCode = _NORMAL;

		String outMsg = null;
		gszMsgBuf = "";
		gEvtTimestamp = Util.getCurrentTimestamp();
		gEvtTimeD = Util.getDoubleTime();
		gEvtSeqID = GUIDGenerator.javaGUID();
		inTrx = new XpmoveinI();
		outTrx = new XpmoveinO();
		outTrx.setTrx_id(TX_NAME);
		outTrx.setType_id(TRX_OUT);
		outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));
		mqOpe = new MqOpe();

		try {
			inTrx = (XpmoveinI) mqOpe.inStr2Obj(strInMsg, inTrx);
			logger.info("InTrx is " + strInMsg);

			rtnCode = subMainProc2();
			if (rtnCode != _NORMAL) {
				TransactionAspectSupport.currentTransactionStatus()
						.setRollbackOnly();
				outTrx.setRtn_code(String.valueOf(rtnCode));
				outTrx.setRtn_mesg(gszMsgBuf);
			} else {
				outTrx.setRtn_code(String.valueOf(RETURN_CODE_OK));
				outTrx.setRtn_mesg(RETURN_MESG_OK);
				retPrdInfoDao.flushCurSession();
			}
		} catch (Exception ex) {
			TransactionAspectSupport.currentTransactionStatus()
					.setRollbackOnly();
			outTrx.setRtn_mesg(ex.toString());
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			logger.error(Util.stackTraceToString(ex));
		} finally {
			outMsg = mqOpe.obj2Str(outTrx);
		}
		return outMsg;
	}

	/**
	 * New all gloabl values
	 */
	private void iniGloablValues() {
		gBis_tool = new Bis_tool();
		gRet_box = new Ret_box();
		gRet_prd_infoList = new ArrayList<Ret_prd_info>();

		gNx_bis_path = new Bis_path();
		gNx_bis_pathi = new Bis_pathi();
		gNx_bis_ope = new Bis_ope();
		gNxRouteObj = new RouteObj();
	}

	/**
	 * All move in logic
	 * 
	 * @return
	 */
	private long subMainProc2() {
		long rtnCode = _NORMAL;

		// New all gloabl values
		iniGloablValues();

		// Get tool group info
		rtnCode = getToolgInfo();
		if (rtnCode != _NORMAL) {
			return rtnCode;
		}

		// Get box info
		rtnCode = getRetBoxInfo();
		if (rtnCode != _NORMAL) {
			return rtnCode;
		}

		// Get sht list info
		rtnCode = getPrdListInfo();
		if (rtnCode != _NORMAL) {
			return rtnCode;
		}

		// Change lot id for DMIC
		// rtnCode = lotChangeProc();
		// if (rtnCode != _NORMAL) {
		// return rtnCode;
		// }

		// Check sht list(status & mix)
		rtnCode = chkShtList();
		if (rtnCode != _NORMAL) {
			return rtnCode;
		}

		//制程发生变化,必须要做半成品入库和出库
	/*	rtnCode = checkChangeProc();
		if(rtnCode != _NORMAL){
			return rtnCode;
		}*/
		
		// Get next path & ope info
		rtnCode = getNextPathInfo();
		if (rtnCode != _NORMAL) {
			return rtnCode;
		}

		// Get PRST DATA info ==> gtoolType
		// Not enable now
		// rtnCode = getDataPRST(gNx_bis_ope.getProc_id());
		// if(rtnCode != _NORMAL){
		// return rtnCode;
		// }

		// Real move in process
		rtnCode = moveInProc();
		if (rtnCode != _NORMAL) {
			return rtnCode;
		}

		// Set box infomation
		rtnCode = setBoxInfo();
		if (rtnCode != _NORMAL) {
			return rtnCode;
		}

		return rtnCode;
	}

	/**
	 * Change lot id for DMIC Use move in box_id as lot_id
	 * 
	 * @return
	 */
	private long lotChangeProc() {
		long rtnCode = _NORMAL;
		String nx_proc_id = gRet_prd_infoList.get(0).getNx_proc_id_fk();
		if (!"DMIC".equals(nx_proc_id)) {
			return rtnCode;
		}

		// Chnage Lot ID
		String boxID = inTrx.getBox_id();
		for (int i = 0; i < gRet_prd_infoList.size(); i++) {
			gRet_prd_infoList.get(i).setLot_id(boxID);

			gRet_prd_infoList.get(i).setEvt_cate(EVT_CHNAGE_LOT);
			gRet_prd_infoList.get(i).setEvt_timestamp(gEvtTimestamp);
			gRet_prd_infoList.get(i).setEvt_time_d(gEvtTimeD);
			gRet_prd_infoList.get(i).setEvt_seq_id(gEvtSeqID);
			gRet_prd_infoList.get(i).setEvt_usr(inTrx.getEvt_usr());

			retPrdInfoDao.update(gRet_prd_infoList.get(i));
		}

		return rtnCode;
	}

	/**
	 * Get tool group info by tool id
	 * 
	 * @param ope_id
	 * @param ope_ver
	 * @return SimpleRtnBeen {String}
	 */
	private SimpleRtnBeen getToolgId(String ope_id, String ope_ver) {
		Bis_ope bis_ope = new Bis_ope();
		Bis_opeId bis_opeId = new Bis_opeId();
		bis_opeId.setOpe_id(ope_id);
		bis_opeId.setOpe_ver(ope_ver);

		bis_ope = bisOpeDao.get(bis_opeId);
		if (bis_ope == null) {
			gszMsgBuf = new NotFoundRtnBeen(bis_opeId.toString(), Bis_ope.class)
					.toString();
			return new SimpleRtnBeen(E_BIS_TOOL_GR + E_READ_NOT_FOUND);
		}

		return new SimpleRtnBeen(bis_ope.getToolg_id());
	}

	/**
	 * Get tool info
	 * 
	 * @return
	 */
	private long getToolgInfo() {
		long rtnCode = _NORMAL;
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);

		rtnBeen = getToolgId(inTrx.getCr_ope_id(), inTrx.getCr_ope_ver());
		if (rtnBeen.getlRc() != _NORMAL) {
			return rtnBeen.getlRc();
		} else {
			gToolG = (String) rtnBeen.getObj();
		}

		if(inTrx.getTool_id() != null){
			gBis_tool = bisToolDao.get(inTrx.getTool_id());
			if (gBis_tool == null) {
				gszMsgBuf = new NotFoundRtnBeen(inTrx.getTool_id(), Bis_tool.class).toString();
				return rtnCode;
			}
		}

		return rtnCode;
	}

	/**
	 * Get box info
	 * 
	 * @return
	 */
	private long getRetBoxInfo() {
		long rtnCode = _NORMAL;
		String boxID = inTrx.getBox_id();
		if (Util.isSpaceCheck(boxID)) {
			gszMsgBuf = "Box ID can't be null!";
			return E_XPMOVEIN_BOX_ID_EMPTY;
		}

		gRet_box = retBoxDao.getWithLock(boxID, LockMode.UPGRADE);
		if (null == gRet_box) {
			gszMsgBuf = new NotFoundRtnBeen(boxID, Ret_box.class).toString();
			return E_RET_BOX + E_READ_NOT_FOUND;
		}
		// Check status
		if (!PROC_WAIT.equals(gRet_box.getBox_stat())) {
			gszMsgBuf = "箱子 [" + boxID + "]的状态为 [" + gRet_box.getBox_stat()
					+ "], 必须是 [" + PROC_WAIT + "]!";
			return E_XPMOVEIN_BOX_STATUS_INVALIED;
		}

		// Check valid flag
		if (!_YES.equals(gRet_box.getValid_flg())) {
			gszMsgBuf = "箱子 [" + boxID + "]的有效标记为[" + gRet_box.getValid_flg()
					+ "],必须是 [" + _YES + "]!";
			return BOX_INVALIED;
		}

		// Check next tool/G
		if (!gToolG.equals(gRet_box.getNx_toolg_id_fk())) {
			gszMsgBuf = "箱子 [" + gRet_box.getBox_id() + "] 属于设备组["
					+ gRet_box.getNx_toolg_id_fk() + "], 但是站点["
					+ inTrx.getCr_ope_id() + "][" + inTrx.getCr_ope_ver()
					+ "] 绑定的是 [" + gToolG + "]设备组, 请检查!";
			return E_XPMOVEIN_BOX_TOOLG_NOT_MATCH;
		}
		if (inTrx.getCr_ope_id().equals(gRet_box.getNx_ope_id_fk())
				&& inTrx.getCr_ope_ver().equals(gRet_box.getNx_ope_ver_fk())) {
			;
		} else {
			gszMsgBuf = "箱子 [" + gRet_box.getBox_id() + "]的站点为 ["
					+ gRet_box.getNx_ope_id_fk() + "]["
					+ gRet_box.getNx_ope_ver_fk() + "], 和选择的站点 ["
					+ inTrx.getCr_ope_id() + "][" + inTrx.getCr_ope_ver()
					+ "]不匹配,请检查!";
			return E_XPMOVEIN_BOX_OPE_NOT_MATCH;
		}
		return rtnCode;
	}

	/**
	 * Get sht list
	 * 
	 * @return
	 */
	private long getPrdListInfo() {
		long rtnCode = _NORMAL;
		String boxID = inTrx.getBox_id();
		gRet_prd_infoList = retPrdInfoDao.getShtListInfo(boxID);
		if (null == gRet_prd_infoList) {
			gszMsgBuf = "箱子 [" + boxID + "]中没有产品,请检查 !";
			return E_RET_PRD_INFO + E_READ_NOT_FOUND;
		}
		Ret_prd_info retPrdInfo = gRet_prd_infoList.get(0);
		Ret_prd_inId ret_prd_inId =new Ret_prd_inId();
		String mtrl_box_id_fk= retPrdInfo.getMtrl_box_id_fk();
		ret_prd_inId.setMtrl_box_id(mtrl_box_id_fk);
		ret_prd_inId.setCus_id("006");
		Ret_prd_in ret_prd_in = retPrdInDao.get(ret_prd_inId);
		if ("UNT-000BZ".equals(retPrdInfo.getNx_ope_id_fk()) && ret_prd_in !=null){
		     Ret_wo ret_wo= retWoDao.get(ret_prd_in.getWo_id_fk());
			String sqlforDM="from Bis_path where path_id_fk ='" + ret_wo.getPath_id_fk()+ "' ";
			List <Bis_path> bis_paths =bisPathDao.find(sqlforDM);
			String DMFlg="";
			for(int i=0;i<bis_paths.size();i++){
				if (bis_paths.get(i).getCr_ope_id_fk().contains("DM")){
					DMFlg="Y";
					break;
				}else{
					DMFlg="N";
				}
			}
			Ret_box ret_box = retBoxDao.get(mtrl_box_id_fk);
			if(!"Y".equals(ret_box.getExt_17())){
				gszMsgBuf="请联系主管到9F00页面确认抛光参数!!!";
				return E_RET_BOX;
			}
			if(!"Y".equals(ret_box.getExt_18())){
			gszMsgBuf="请联系主管到9F00页面确认IQC板厚信息!!!";
			    return E_RET_BOX;
			}
			if("Y".equals(ret_box.getExt_19())){
				gszMsgBuf="请联系主管到9F00页面确认成检板厚信息!!!";
				return E_RET_BOX;
			}
			if("Y".equals(DMFlg) && !"Y".equals(ret_box.getExt_20())){
				gszMsgBuf="请联系主管到9F00页面确认镀膜信息!!!";
				return E_RET_BOX;
			}
		}





		return rtnCode;
	}

	/**
	 * Check sht list info
	 * 
	 * @return
	 */
	private long chkShtList() {
		long rtnCode = _NORMAL;

		// Check total count
		if (gRet_prd_infoList.size() != gRet_box.getPrd_qty()) {
			gszMsgBuf = "箱子的数量为 [" + gRet_box.getPrd_qty() + "] 和箱子中产品的数量["
					+ gRet_prd_infoList.size() + "] 不匹配，请检查!";
			return E_XPMOVEIN_BOX_AND_PRD_COUNT_NOT_MATCH;
		}

		// Check mix
		for (int i = 0; i < gRet_prd_infoList.size(); i++) {

			// Status must WAIT
			if (!PROC_WAIT.equals(gRet_prd_infoList.get(i).getPrd_stat())) {
				gszMsgBuf = "产品 [" + gRet_prd_infoList.get(i).getPrd_seq_id()
						+ "] 状态为[" + gRet_prd_infoList.get(i).getPrd_stat()
						+ "] ，必须是 [" + PROC_WAIT + "]!";
				return E_XPMOVEIN_SHT_STATUS_INVALIED;
			}

			// Check wip bank
			if (BANK_IN.equals(gRet_prd_infoList.get(i).getBnk_flg())) {
				gszMsgBuf = "产品 [" + gRet_prd_infoList.get(i).getPrd_seq_id()
						+ "] 当前在在制品仓，请先从在制品仓转出!";
				return E_XPMOVEIN_PRD_IN_WIP_BNK;
			}

			if (inTrx.getCr_ope_id().equals(
					gRet_prd_infoList.get(i).getNx_ope_id_fk())
					&& inTrx.getCr_ope_ver().equals(
							gRet_prd_infoList.get(i).getNx_ope_ver_fk())) {
				;
			} else {
				gszMsgBuf = "产品 [" + gRet_prd_infoList.get(i).getPrd_seq_id()
						+ "]的站点 [" + gRet_box.getNx_ope_id_fk() + "]["
						+ gRet_box.getNx_ope_ver_fk() + "], 和选择的站点 ["
						+ inTrx.getCr_ope_id() + "][" + inTrx.getCr_ope_ver()
						+ "]不匹配，请检查!";
				return E_XPMOVEIN_PRD_OPE_NOT_MATCH;
			}

			// NX_PATH_ID_FK
			if (!gRet_prd_infoList.get(0).getNx_path_id_fk()
					.equals(gRet_prd_infoList.get(i).getNx_path_id_fk())) {
				gszMsgBuf = "Mix 下一制程工艺路线 ["
						+ gRet_prd_infoList.get(0).getPrd_seq_id() + "]["
						+ gRet_prd_infoList.get(0).getNx_path_id_fk()
						+ "] vs [" + gRet_prd_infoList.get(i).getPrd_seq_id()
						+ "][" + gRet_prd_infoList.get(i).getNx_path_id_fk()
						+ "]!";
				return E_XPMOVEIN_SHT_MIX_NX_PATH_ID;
			}

			// NX_PATH_VER_FK
			if (!gRet_prd_infoList.get(0).getNx_path_ver_fk()
					.equals(gRet_prd_infoList.get(i).getNx_path_ver_fk())) {
				gszMsgBuf = "Mix 下一制程工艺路线版本 ["
						+ gRet_prd_infoList.get(0).getPrd_seq_id() + "]["
						+ gRet_prd_infoList.get(0).getNx_path_ver_fk()
						+ "] vs [" + gRet_prd_infoList.get(i).getPrd_seq_id()
						+ "][" + gRet_prd_infoList.get(i).getNx_path_ver_fk()
						+ "]!";
				return E_XPMOVEIN_SHT_MIX_NX_PATH_VER;
			}

			// NX_OPE_NO_FK
			if (!gRet_prd_infoList.get(0).getNx_ope_no_fk()
					.equals(gRet_prd_infoList.get(i).getNx_ope_no_fk())) {
				gszMsgBuf = "Mix 下一制程站点 ["
						+ gRet_prd_infoList.get(0).getPrd_seq_id() + "]["
						+ gRet_prd_infoList.get(0).getNx_ope_no_fk() + "] vs ["
						+ gRet_prd_infoList.get(i).getPrd_seq_id() + "]["
						+ gRet_prd_infoList.get(i).getNx_ope_no_fk() + "]!";
				return E_XPMOVEIN_SHT_MIX_NX_OPE_NO;
			}

			// NX_OPE_ID_FK
			if (!gRet_prd_infoList.get(0).getNx_ope_id_fk()
					.equals(gRet_prd_infoList.get(i).getNx_ope_id_fk())) {
				gszMsgBuf = "Mix 下一制程站点代码 ["
						+ gRet_prd_infoList.get(0).getPrd_seq_id() + "]["
						+ gRet_prd_infoList.get(0).getNx_ope_id_fk() + "] vs ["
						+ gRet_prd_infoList.get(i).getPrd_seq_id() + "]["
						+ gRet_prd_infoList.get(i).getNx_ope_id_fk() + "]!";
				return E_XPMOVEIN_SHT_MIX_NX_OPE_ID;
			}

			// NX_OPE_VER_FK
			if (!gRet_prd_infoList.get(0).getNx_ope_ver_fk()
					.equals(gRet_prd_infoList.get(i).getNx_ope_ver_fk())) {
				gszMsgBuf = "Mix 下一制程站点版本 ["
						+ gRet_prd_infoList.get(0).getPrd_seq_id() + "]["
						+ gRet_prd_infoList.get(0).getNx_ope_ver_fk()
						+ "] vs [" + gRet_prd_infoList.get(i).getPrd_seq_id()
						+ "][" + gRet_prd_infoList.get(i).getNx_ope_ver_fk()
						+ "]!";
				return E_XPMOVEIN_SHT_MIX_NX_OPE_VER;
			}

			// NX_PROC_ID_FK
			if (!gRet_prd_infoList.get(0).getNx_proc_id_fk()
					.equals(gRet_prd_infoList.get(i).getNx_proc_id_fk())) {
				gszMsgBuf = "Mix 下一制程代码 ["
						+ gRet_prd_infoList.get(0).getPrd_seq_id() + "]["
						+ gRet_prd_infoList.get(0).getNx_proc_id_fk()
						+ "] vs [" + gRet_prd_infoList.get(i).getPrd_seq_id()
						+ "][" + gRet_prd_infoList.get(i).getNx_proc_id_fk()
						+ "]!";
				return E_XPMOVEIN_SHT_MIX_NX_PROC_ID;
			}

			// NX_TOOLG_ID_FK
			if (!gRet_prd_infoList.get(0).getNx_toolg_id_fk()
					.equals(gRet_prd_infoList.get(i).getNx_toolg_id_fk())) {
				gszMsgBuf = "Mix 下一制程设备组 ["
						+ gRet_prd_infoList.get(0).getPrd_seq_id() + "]["
						+ gRet_prd_infoList.get(0).getNx_toolg_id_fk()
						+ "] vs [" + gRet_prd_infoList.get(i).getPrd_seq_id()
						+ "][" + gRet_prd_infoList.get(i).getNx_toolg_id_fk()
						+ "]!";
				return E_XPMOVEIN_SHT_MIX_NX_TOOLG_ID;
			}

			// NX_TOOL_ID_FK
			// if(
			// !gRet_prd_infoList.get(0).getNx_tool_id_fk().equals(gRet_prd_infoList.get(i).getNx_tool_id_fk())){
			// gszMsgBuf = "Mix NX_TOOL_ID ["
			// + gRet_prd_infoList.get(0).getPrd_seq_id() + "]["
			// + gRet_prd_infoList.get(0).getNx_tool_id_fk()
			// + "] vs [" + gRet_prd_infoList.get(i).getPrd_seq_id()
			// + "][" + gRet_prd_infoList.get(i).getNx_tool_id_fk()
			// + "]!";
			// return E_XPMOVEIN_SHT_MIX_NX_TOOL_ID;
			// }
		}

		return rtnCode;
	}

	/**
	 * Get next path & ope info
	 * 
	 * @return
	 */
	private long getNextPathInfo() {
		long rtnCode = _NORMAL;
		Bis_pathiId bis_pathiId = new Bis_pathiId();

		gNxRouteObj = bisPathDao.getNxPath(gRet_prd_infoList.get(0)
				.getNx_path_id_fk(), gRet_prd_infoList.get(0)
				.getNx_path_ver_fk(), gRet_prd_infoList.get(0)
				.getNx_ope_no_fk());
		if (_NORMAL == gNxRouteObj.getLRc()) {
			gNx_bis_path = gNxRouteObj.getPath();
			gNx_bis_ope = gNxRouteObj.getOpe();

			bis_pathiId.setPath_id(gNx_bis_path.getId().getPath_id_fk());
			bis_pathiId.setPath_ver(gNx_bis_path.getId().getPath_ver_fk());

			gNx_bis_pathi = bisPathiDao.get(bis_pathiId);
			if (null == gNx_bis_pathi) {
				gszMsgBuf = new NotFoundRtnBeen(bis_pathiId.toString(),
						Bis_pathi.class).toString();
				return E_BIS_PATHI + E_READ_NOT_FOUND;
			}
		} else if (PATH_IS_FINISH == gNxRouteObj.getLRc()) {
			logger.info(gRet_prd_infoList.get(0).getPrd_seq_id()
					+ " in last ope in path!");
		} else {
			rtnCode = gNxRouteObj.getLRc();
			gszMsgBuf = "Get next path info error: "
					+ gNxRouteObj.getGszMsgBuf();
		}

		return rtnCode;
	}

	/**
	 * Just do move in operation
	 * 
	 * @return
	 */
	private long moveInProc() {
		long rtnCode = _NORMAL;

		for (int i = 0; i < gRet_prd_infoList.size(); i++) {

			// Set logon data
			gRet_prd_infoList.get(i).setPv_box_id_fk(
					gRet_prd_infoList.get(i).getBox_id_fk());
			gRet_prd_infoList.get(i).setBox_id_fk(_SPACE);
			gRet_prd_infoList.get(i).setPv_prd_stat_fk(
					gRet_prd_infoList.get(i).getPrd_stat());
			gRet_prd_infoList.get(i).setPrd_stat(PROC_IN_PROCESS);
			gRet_prd_infoList.get(i).setPrd_grade(_SPACE);    //入账将Prd_grade置空 
			gRet_prd_infoList.get(i).setTool_id_fk(_SPACE);

			// Event
			gRet_prd_infoList.get(i).setEvt_cate(EVT_LGON);
			gRet_prd_infoList.get(i).setEvt_timestamp(gEvtTimestamp);
			gRet_prd_infoList.get(i).setEvt_time_d(gEvtTimeD);
			gRet_prd_infoList.get(i).setEvt_seq_id(gEvtSeqID);
			gRet_prd_infoList.get(i).setEvt_usr(inTrx.getEvt_usr());
			gRet_prd_infoList.get(i).setPv_logon_usr(
					gRet_prd_infoList.get(i).getLogon_usr());
			gRet_prd_infoList.get(i).setPv_logon_timestamp(
					gRet_prd_infoList.get(i).getLogon_timestamp());
			gRet_prd_infoList.get(i).setLogon_usr(inTrx.getEvt_usr());
			gRet_prd_infoList.get(i).setLogon_timestamp(gEvtTimestamp);

			// Next -> cur
			gRet_prd_infoList.get(i).setCr_path_id_fk(
					gRet_prd_infoList.get(i).getNx_path_id_fk());
			gRet_prd_infoList.get(i).setCr_path_ver_fk(
					gRet_prd_infoList.get(i).getNx_path_ver_fk());
			gRet_prd_infoList.get(i).setCr_ope_no_fk(
					gRet_prd_infoList.get(i).getNx_ope_no_fk());
			gRet_prd_infoList.get(i).setCr_ope_id_fk(
					gRet_prd_infoList.get(i).getNx_ope_id_fk());
			gRet_prd_infoList.get(i).setCr_ope_ver_fk(
					gRet_prd_infoList.get(i).getNx_ope_ver_fk());
			gRet_prd_infoList.get(i).setCr_proc_id_fk(
					gRet_prd_infoList.get(i).getNx_proc_id_fk());
			gRet_prd_infoList.get(i).setCr_tool_id_fk(_SPACE);
			gRet_prd_infoList.get(i).setCr_toolg_id_fk(
					gRet_prd_infoList.get(i).getNx_toolg_id_fk());

			// Next path & ope
			if (PATH_IS_FINISH == gNxRouteObj.getLRc()) { // COMP
				gRet_prd_infoList.get(i).setNx_ope_no_fk(_SPACE);
				gRet_prd_infoList.get(i).setNx_ope_id_fk(_SPACE);
				gRet_prd_infoList.get(i).setNx_ope_ver_fk(_SPACE);
				gRet_prd_infoList.get(i).setNx_proc_id_fk(_SPACE);
				gRet_prd_infoList.get(i).setNx_tool_id_fk(_SPACE);
				gRet_prd_infoList.get(i).setNx_toolg_id_fk(_SPACE);
			} else {
				gRet_prd_infoList.get(i).setNx_path_id_fk(
						gNx_bis_path.getId().getPath_id_fk());
				gRet_prd_infoList.get(i).setNx_path_ver_fk(
						gNx_bis_path.getId().getPath_ver_fk());
				gRet_prd_infoList.get(i).setNx_ope_no_fk(
						gNx_bis_path.getId().getCr_ope_no());
				gRet_prd_infoList.get(i).setNx_ope_id_fk(
						gNx_bis_ope.getId().getOpe_id());
				gRet_prd_infoList.get(i).setNx_ope_ver_fk(
						gNx_bis_ope.getId().getOpe_ver());
				gRet_prd_infoList.get(i).setNx_proc_id_fk(
						gNx_bis_ope.getProc_id());
				gRet_prd_infoList.get(i).setNx_toolg_id_fk(
						gNx_bis_ope.getToolg_id());
				gRet_prd_infoList.get(i).setNx_tool_id_fk(_SPACE);
			}

			// Sv path info
			if (PATH_TYPE_MAIN.equals(gNx_bis_pathi.getPath_cate())) {
				gRet_prd_infoList.get(i).setSv_path_id_fk(_SPACE);
				gRet_prd_infoList.get(i).setSv_path_ver_fk(_SPACE);
				gRet_prd_infoList.get(i).setSv_ope_id_fk(_SPACE);
				gRet_prd_infoList.get(i).setSv_ope_no_fk(_SPACE);
				gRet_prd_infoList.get(i).setSv_ope_ver_fk(_SPACE);
			}

			// Clear process start time & end time
			gRet_prd_infoList.get(i).setP1_timestamp(null);
			gRet_prd_infoList.get(i).setP2_timestamp(null);
//			gRet_prd_infoList.get(i).setP3_timestamp(null);不清空，记录JBPK的时间
//			gRet_prd_infoList.get(i).setP4_timestamp(null);不清空，记录JBZJ和DMZJ的时间
			gRet_prd_infoList.get(i).setP5_timestamp(null);
			gRet_prd_infoList.get(i).setP6_timestamp(null);
			gRet_prd_infoList.get(i).setProc_flg("Y");

			retPrdInfoDao.update(gRet_prd_infoList.get(i));
		}

		return rtnCode;
	}

	private long getDataPRST(String proc_id) {
		long rtnCode = _NORMAL;
		MqOpe mqOpeT = new MqOpe();
		String outMsg = "";

		XpinqcodI xpinqcodI = new XpinqcodI();
		XpinqcodO xpinqcodO = new XpinqcodO();

		xpinqcodI.setTrx_id(T_XPINQCOD);
		xpinqcodI.setType_id(TRX_IN);
		xpinqcodI.setAction_flg("I");
		xpinqcodI.setData_cate("PRST");
		xpinqcodI.setData_id(proc_id);

		outMsg = xpinqcodService.subMainProc(mqOpeT.obj2Str(xpinqcodI));
		xpinqcodO = (XpinqcodO) mqOpe.inStr2Obj(outMsg, xpinqcodO);
		if (null == xpinqcodO) {
			gszMsgBuf = new NotFoundRtnBeen("PRST", Bis_data.class).toString();
			return E_BIS_DATA + E_READ_NOT_FOUND;
		}

		if (1 != xpinqcodO.getOary().size()) {
			gszMsgBuf = "DATA_CATE [PRST], DATA_ID[" + proc_id
					+ "] get record count [" + xpinqcodO.getOary().size()
					+ "] invalied, must only one!";
			return E_XPMOVEIN_INVALIED_DATA_SET_PRST;
		}

		if (TOOL_TYPE_COMMONE.equals(xpinqcodO.getOary().get(0).getExt_1())) {
			gtoolType = TOOL_TYPE_COMMONE;
		} else if (TOOL_TYPE_IDENPENDENT.equals(xpinqcodO.getOary().get(0)
				.getExt_1())) {
			gtoolType = TOOL_TYPE_IDENPENDENT;
		} else {
			gszMsgBuf = "DATA_CATE [PRST], DATA_ID[" + proc_id + "]'s EXT_2 ["
					+ xpinqcodO.getOary().get(0).getExt_1()
					+ "] invalied, must IN[" + TOOL_TYPE_COMMONE + ","
					+ TOOL_TYPE_IDENPENDENT + "]!";
			return E_XPMOVEIN_INVALIED_DATA_SET_PRST;
		}

		return _NORMAL;
	}

	/**
	 * Set box info
	 * 
	 * @return
	 */
	private long setBoxInfo() {
		long rtnCode = _NORMAL;

		// Now just set box to EMPT
		gRet_box = retBoxDao.setBoxEmpt(gRet_box);

		// TODO test
		logger.info(gRet_box.getGroup_id().length());

		// if(TOOL_TYPE_COMMONE.equals(gtoolType)){
		// gRet_box.setBox_stat(PROC_EMPTY);
		// gRet_box.setReal_emp(EXIST_CARRIER);
		// }else if(TOOL_TYPE_IDENPENDENT.equals(gtoolType)){
		// gRet_box.setBox_stat(PROC_IN_PROCESS);
		// gRet_box.setReal_emp(EXIST_CARRIER);
		// }

		retBoxDao.update(gRet_box);

		return rtnCode;
	}
	
	private long checkChangeProc(){
		long rtnCode = _NORMAL;
		Ret_prd_info firstSht = gRet_prd_infoList.get(0);
		if(firstSht.getEvt_cate().equals("WHRT")){
			return rtnCode;
		}
		if(retPrdInfoDao.isChangeProcFlg(firstSht)){
			for (Ret_prd_info retPrdInfo : gRet_prd_infoList) {
				if(!"Y".equals(retPrdInfo.getQrs_ovr_flg())){
					outTrx.setRtn_code(E_XPMOVEIN_PROC_CHANGE_NOT_WIP_IN_OUT+"");
					gszMsgBuf ="产品制程已经变化,但没有做半成品入库和出库,不能入账";
					return E_XPMOVEIN_PROC_CHANGE_NOT_WIP_IN_OUT;
				}else{
					retPrdInfo.setQrs_ovr_flg("N");
				}
			}
		}
		return rtnCode;
	}

}
